package com.sdnu.service.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdnu.common.Result.R;
import com.sdnu.service.entity.quest.CheckInData;
import com.sdnu.service.entity.quest.vo.CheckInDataQuery;
import com.sdnu.service.entity.quest.vo.CheckInDataVo;
import com.sdnu.service.service.CheckInDataService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Konjacer
 * @since 2022-02-24
 */
@RestController
@RequestMapping("/ucenter/check_in_data")
@CrossOrigin
public class CheckInDataController {
    @Autowired
    private CheckInDataService checkInDataService;

    @ApiOperation("得到所有签到数据")
    @GetMapping("getAll")
    public R getAll(){
        List<CheckInDataVo> list = checkInDataService.getAll();
        return R.ok().data("items",list);
    }

    @ApiOperation("添加签到数据")
    @PostMapping("add")
    public R addRecord(@RequestBody CheckInData checkInData){
        checkInDataService.save(checkInData);
        //日志
        System.out.println(new Date()+":"+checkInData.getName()+"-->进行了签到！");
        return R.ok();
    }

    @ApiOperation("通过id修改签到数据")
    @PutMapping("update")
    public R updateRecord(@RequestBody CheckInData checkInData){
        boolean b = checkInDataService.updateById(checkInData);
        return b?R.ok():R.error();
    }

    @ApiOperation("通过id删除签到数据")
    @DeleteMapping("delete/{id}")
    public R delete(@PathVariable String id){
        boolean b = checkInDataService.removeById(id);
        return b?R.ok():R.error();
    }

    @ApiOperation("通过id获取签到数据")
    @GetMapping("get/{id}")
    public R getRecordById(@PathVariable String id){
        CheckInData item = checkInDataService.getById(id);
        return R.ok().data("item",item);
    }

    @ApiOperation("分页查询签到数据")
    @PostMapping("pageSelect/{current}/{limit}")
    public R pageSelect(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable long current,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit,

            @ApiParam("查询条件")
            @RequestBody CheckInDataQuery checkInDataQuery
    ){
        Page<CheckInData> page = new Page<>(current, limit);
//        得到构造条件
        QueryWrapper<CheckInData> wrapper = checkInDataService.querySelect(checkInDataQuery);
        wrapper.orderByDesc("gmt_create");

        List<CheckInDataVo> finalList = checkInDataService.pageSelect(page, wrapper);

        //经过处理，原始数据封装在page中
        //total是所有的记录数，不是records中的记录数
        long total = page.getTotal();

        return R.ok().data("total",total).data("items",finalList);
    }
}

